package com.wen.common.config;


import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;

/**
 * 跨域问题
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")

                .allowedOrigins("*")

                .allowCredentials(false)

                .allowedMethods("GET", "POST", "DELETE", "PUT","OPTIONS")

                .allowedHeaders("*")

                .maxAge(3600);

    }

//    @Bean
//    public FilterRegistrationBean<Filter> filterRegistrationBean() {
//        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(corsFilter());
//        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        return registrationBean;
//    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//
//        UrlBasedCorsConfigurationSource urlSource = new UrlBasedCorsConfigurationSource();
//        urlSource.registerCorsConfiguration("/**", buildConfig());
//        return new CorsFilter(urlSource);
//    }
//
//    private CorsConfiguration buildConfig() {
//        CorsConfiguration configuration = new CorsConfiguration();
//        configuration.addAllowedHeader("*");
//        configuration.addAllowedMethod("*");
//        configuration.addAllowedOrigin("*");
////        configuration.addExposedHeader("token");
//
//        configuration.setMaxAge(24L * 60L * 60L); //跨域请求有效时长
////        configuration.setAllowCredentials(true); // 设置可携带cookie凭据
//        return configuration;
//    }

}